package com.xyht.medicine_management_system.modules.manufacturer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.manufacturer.entity.MmsManufacturer;
import com.xyht.medicine_management_system.modules.manufacturer.entity.req.MmsManufacturerReq;
import com.xyht.medicine_management_system.modules.manufacturer.entity.resp.MmsManufacturerResp;
import com.xyht.medicine_management_system.modules.manufacturer.mapper.MmsManufacturerMapper;
import com.xyht.medicine_management_system.modules.manufacturer.service.MmsManufacturerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.medicine_management_system.modules.user.entity.MmsUser;
import com.xyht.medicine_management_system.modules.user.entity.resp.UserinfoResp;
import com.xyht.medicine_management_system.modules.user.mapper.MmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 厂家管理 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-11-29
 */
@Service
public class MmsManufacturerServiceImpl extends ServiceImpl<MmsManufacturerMapper, MmsManufacturer> implements MmsManufacturerService {
    @Resource
    private MmsManufacturerMapper mmsManufacturerMapper;
    @Resource
    private MmsUserMapper mmsUserMapper;

    @Override
    public ResponseResult addManufacturer(MmsManufacturerReq manufacturerReq, String user_id) {
        if (isNullOrEmpty(manufacturerReq.getMfName())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsManufacturer addManufacturer = new MmsManufacturer();
        BeanUtils.copyProperties(manufacturerReq,addManufacturer);

        addManufacturer.setMfCreator(user_id);
        addManufacturer.setLastUpdateUser(user_id);

        mmsManufacturerMapper.insert(addManufacturer);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getManufactureDetail(String id) {
        MmsManufacturer manufacturer = mmsManufacturerMapper.selectById(id);
        if (manufacturer == null){
            return CommonResult.failed(CommonCodeEnum.MANUFACTURE_NOT_EXIST);
        }
        MmsManufacturerResp manufacturerResp = new MmsManufacturerResp();
        BeanUtils.copyProperties(manufacturer,manufacturerResp);

        MmsUser creator = mmsUserMapper.selectById(manufacturer.getMfCreator());
        if (creator != null){
            UserinfoResp userinfoResp = new UserinfoResp();
            BeanUtils.copyProperties(creator,userinfoResp);
            manufacturerResp.setCreatorUserinfo(userinfoResp);
        }

        MmsUser lastUser = mmsUserMapper.selectById(manufacturer.getMfCreator());
        if (lastUser != null){
            UserinfoResp userinfoResp = new UserinfoResp();
            BeanUtils.copyProperties(lastUser,userinfoResp);
            manufacturerResp.setLastUserinfo(userinfoResp);
        }
        return CommonResult.success(manufacturerResp);
    }

    @Override
    public ResponseResult getManufactureList(Integer pageNum, Integer pageSize, String searchStr) {
        Page<MmsManufacturer> page = new Page<>(pageNum,pageSize);
        QueryWrapper<MmsManufacturer> queryWrapper = new QueryWrapper<>();
        if (!isNullOrEmpty(searchStr)){
            queryWrapper.lambda().like(MmsManufacturer::getMfName,searchStr);
        }
        queryWrapper.lambda().orderByDesc(MmsManufacturer::getCreateTime);

        mmsManufacturerMapper.selectPage(page,queryWrapper);
        List<MmsManufacturerResp> list = page.getRecords()
                .stream()
                .map(mmsManufacturer -> {
                    MmsManufacturerResp manufacturerResp = new MmsManufacturerResp();
                    BeanUtils.copyProperties(mmsManufacturer,manufacturerResp);

                    MmsUser creator = mmsUserMapper.selectById(mmsManufacturer.getMfCreator());
                    if (creator != null){
                        UserinfoResp userinfoResp = new UserinfoResp();
                        BeanUtils.copyProperties(creator,userinfoResp);
                        manufacturerResp.setCreatorUserinfo(userinfoResp);
                    }

                    MmsUser lastUser = mmsUserMapper.selectById(mmsManufacturer.getMfCreator());
                    if (lastUser != null){
                        UserinfoResp userinfoResp = new UserinfoResp();
                        BeanUtils.copyProperties(lastUser,userinfoResp);
                        manufacturerResp.setLastUserinfo(userinfoResp);
                    }
                    return manufacturerResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list,total);
    }

    @Override
    public ResponseResult updateManufacture(MmsManufacturerReq manufacturerReq, String user_id) {
        if (isNullOrEmpty(manufacturerReq.getId(),manufacturerReq.getMfName())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsManufacturer manufacturer = mmsManufacturerMapper.selectById(manufacturerReq.getId());
        if (manufacturer == null){
            return CommonResult.failed(CommonCodeEnum.MANUFACTURE_NOT_EXIST);
        }

        MmsManufacturer updateManufacturer = new MmsManufacturer();
        BeanUtils.copyProperties(manufacturerReq,updateManufacturer);

        updateManufacturer.setLastUpdateUser(user_id);
        mmsManufacturerMapper.updateById(updateManufacturer);
        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult deleteManufacture(MmsManufacturerReq manufacturerReq, String user_id) {
        if (isNullOrEmpty(manufacturerReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsManufacturer manufacturer = mmsManufacturerMapper.selectById(manufacturerReq.getId());
        if (manufacturer == null){
            return CommonResult.failed(CommonCodeEnum.MANUFACTURE_NOT_EXIST);
        }

        manufacturer.setLastUpdateUser(user_id);
        mmsManufacturerMapper.updateById(manufacturer);

        mmsManufacturerMapper.deleteById(manufacturerReq.getId());
        return CommonResult.success();
    }
}
